/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.dao.impl;

import com.rbh.risk.dao.WorkgroupDao;
import com.rbh.risk.entity.Workgroup;
import com.rbh.risk.support.SessionFactorySupport;
import com.rbh.risk.util.Constants;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository("workgroupDao")
public class WorkgroupDaoImpl extends SessionFactorySupport implements WorkgroupDao {

    @Override
    public void insert(Workgroup workgroup) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().save(workgroup);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void update(Workgroup workgroup) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().update(workgroup);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void delete(Workgroup workgroup) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().delete(workgroup);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public Workgroup getById(Integer workgroupId) throws DataAccessException {
        Workgroup result = null;
        try {
            result = (Workgroup) super.getSessionFactory().getCurrentSession().get(Workgroup.class, workgroupId);
        } catch (Exception ex) {
            throw ex;
        }
        return result;
    }

    @Override
    public List<Workgroup> getAll() throws DataAccessException {
        List<Workgroup> results = null;
        try {
            Query query = super.getSessionFactory().getCurrentSession().createQuery("from " + Workgroup.class.getName());
            results = query.setCacheable(true).list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }

    @Override
    public List<Workgroup> getChildWorkgroup() throws DataAccessException {
        List<Workgroup> results = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(Workgroup.class);
            criteria.createAlias("workgroupLevel", "lv");
            criteria.add(Restrictions.eq("workgroupStatus", Constants.ENABLED));
            criteria.add(Restrictions.eq("lv.workgroupLevelId", Constants.WORKGROUP_FOR_COUNT));
            criteria.addOrder(Order.asc("workgroupOrderNumber"));
            results = criteria.list();
        } catch (HibernateException | NumberFormatException ex) {
            throw ex;
        }
        return results;
    }

    @Override
    public List<Workgroup> getByWords(String words) throws DataAccessException {
        List<Workgroup> results = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(Workgroup.class);
            criteria.createAlias("workgroupLevel", "lv");

            Criterion findAvaliable = Restrictions.conjunction()
                    .add(Restrictions.eq("workgroupStatus", Constants.ENABLED))
                    .add(Restrictions.eq("lv.workgroupLevelId", Constants.WORKGROUP_FOR_COUNT));
            Criterion findWords = Restrictions.like("workgroupName", words, MatchMode.ANYWHERE);

            if (words != null && !words.isEmpty()) {
                criteria.add(Restrictions.conjunction().add(findAvaliable).add(findWords));
            } else {
                criteria.add(Restrictions.conjunction().add(findAvaliable));
            }

            criteria.addOrder(Order.asc("workgroupOrderNumber"));
            results = criteria.setCacheable(true).list();
        } catch (HibernateException | NumberFormatException ex) {
            throw ex;
        }
        return results;
    }

    @Override
    public List<Workgroup> getByNotEqWords(String words, Workgroup workgroup) throws DataAccessException {
        List<Workgroup> results = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(Workgroup.class);
            criteria.createAlias("workgroupLevel", "lv");
            criteria.add(Restrictions.conjunction()
                    .add(Restrictions.eq("workgroupStatus", Constants.ENABLED))
                    .add(Restrictions.eq("lv.workgroupLevelId", Constants.WORKGROUP_FOR_COUNT))
                    .add(Restrictions.ne("workgroupId", workgroup.getWorkgroupId()))
                    .add(Restrictions.like("workgroupName", words, MatchMode.ANYWHERE)));
            criteria.addOrder(Order.asc("workgroupOrderNumber"));
            results = criteria.list();
        } catch (HibernateException | NumberFormatException ex) {
            throw ex;
        }
        return results;
    }
}
